package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableUser;
import cn.ac.jaas.isc.vegetable.common.security.util.SecurityUtils;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcBase;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcDeviceType;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcDeviceTypeMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcDeviceTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 装置类型表
 *
 * @author 张高昌
 * @date 2022-05-21 18:03:32
 */
@Service
@AllArgsConstructor
public class ArcDeviceTypeServiceImpl extends ServiceImpl<ArcDeviceTypeMapper, ArcDeviceType> implements ArcDeviceTypeService {

    private final ArcEquipmentMapper arcEquipmentMapper;

    @Override
    public R saveDeviceType(ArcDeviceType arcDeviceType) {
        LambdaQueryWrapper<ArcDeviceType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcDeviceType::getEquipmentTypeCode, arcDeviceType.getEquipmentTypeCode())
                .eq(ArcDeviceType::getTypeName, arcDeviceType.getTypeName());
        ArcDeviceType nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("装置类型名称已存在！");
        }
        LambdaQueryWrapper<ArcDeviceType> ecWrapper = Wrappers.lambdaQuery();
        ecWrapper.eq(ArcDeviceType::getTypeCode, arcDeviceType.getTypeCode());
        ArcDeviceType typeModel = baseMapper.selectOne(ecWrapper);
        if (typeModel != null) {
            return R.failed("装置类型编码已存在！");
        }

        VegetableUser user = SecurityUtils.getUser();
        arcDeviceType.setCreateTime(LocalDateTime.now());
        arcDeviceType.setCreateUser(user.getId());
        baseMapper.insert(arcDeviceType);
        return R.ok(true);
    }

    @Override
    public R updateDeviceTypeById(ArcDeviceType arcDeviceType) {
        LambdaQueryWrapper<ArcDeviceType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcDeviceType::getEquipmentTypeCode, arcDeviceType.getEquipmentTypeCode())
                .eq(ArcDeviceType::getTypeName, arcDeviceType.getTypeName())
                .ne(ArcDeviceType::getDeviceTypeId, arcDeviceType.getDeviceTypeId());
        ArcDeviceType nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("装置类型名称已存在！");
        }

        VegetableUser user = SecurityUtils.getUser();
        arcDeviceType.setUpdateTime(LocalDateTime.now());
        arcDeviceType.setUpdateUser(user.getId());
        baseMapper.updateById(arcDeviceType);
        return R.ok(true);
    }

    @Override
    public List<ArcDeviceType> getDeviceTypeAll(String equipmentCode) {
        //根据设备编码查询到这个设备所属的设备类型
        ArcEquipment arcEquipment = arcEquipmentMapper.selectOne(Wrappers.<ArcEquipment>query().lambda().eq(ArcEquipment::getEquipmentCode, equipmentCode));
        //根据设备类型查询到 装置类型
        List<ArcDeviceType> arcDeviceTypes = baseMapper.selectList(Wrappers.<ArcDeviceType>query().lambda().eq(ArcDeviceType::getEquipmentTypeCode, arcEquipment.getEquipmentTypeCode()));

        return arcDeviceTypes;
    }
}
